package cn.basic.social.api.dingtalk.channel.internal.request;

import cn.basic.social.api.CommonConstants;
import cn.basic.social.api.dingtalk.BaseDingRequest;
import cn.basic.social.api.dingtalk.channel.internal.response.InternalGetUserAccessTokenResponse;
import cn.basic.social.api.exception.ApiRuleException;
import cn.basic.social.api.utils.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName InternalGetUserAccessTokenRequest
 * @Description 获取用户token请求
 * https://open.dingtalk.com/document/orgapp/obtain-user-token?spm=ding_open_doc.document.0.0.38e81a0dKnvhmy
 * @Author yk.zlf
 * @Date 2023/10/20 15:08
 */
public class InternalGetUserAccessTokenRequest extends BaseDingRequest<InternalGetUserAccessTokenResponse> implements Serializable {

    private final String requestUrl = "https://api.dingtalk.com/v1.0/oauth2/userAccessToken";
    private String topHttpMethod = CommonConstants.HTTP_METHOD_POST;
    private String topContentType = "application/json";

    /**
     * 应用id。可使用扫码登录应用或者第三方个人小程序的appId。
     * 企业内部应用传应用的AppKey
     * 第三方企业应用传应用的SuiteKey
     * 第三方个人应用传应用的AppId
     */
    private String clientId;

    /**
     * 应用密钥。
     * 企业内部应用传应用的AppSecret
     * 第三方企业应用传应用的SuiteSecret
     * 第三方个人应用传应用的AppSecret
     */
    private String clientSecret;

    /**
     * OAuth 2.0 临时授权码，根据获取登录用户的访问凭证内容，获取临时授权码authCode
     */
    private String code;

    /**
     * 非必填
     * OAuth2.0刷新令牌，从返回结果里面获取。 过期时间是30天
     */
    private String refreshToken;

    /**
     * 非必填
     * 如果使用授权码换token，传authorization_code。
     * 如果使用刷新token换用户token，传refresh_token。
     */
    private String grantType;

    public InternalGetUserAccessTokenRequest() {
    }

    public InternalGetUserAccessTokenRequest(String clientId, String clientSecret, String code, String refreshToken, String grantType) {
        this.clientId = clientId;
        this.clientSecret = clientSecret;
        this.code = code;
        this.refreshToken = refreshToken;
        this.grantType = grantType;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public String getGrantType() {
        return grantType;
    }

    public void setGrantType(String grantType) {
        this.grantType = grantType;
    }

    @Override
    public String getTopHttpMethod() {
        return this.topHttpMethod;
    }

    @Override
    public void setTopHttpMethod(String topHttpMethod) {
        this.topHttpMethod = topHttpMethod;
    }

    @Override
    public Map<String, String> getTextParams() {
        Map<String, String> params = new HashMap<>();
        params.put("clientId", this.clientId);
        params.put("clientSecret", this.clientSecret);
        params.put("code", this.code);
        params.put("refreshToken", this.refreshToken);
        params.put("grantType", this.grantType);
        return params;
    }

    @Override
    public Class<InternalGetUserAccessTokenResponse> getResponseClass() {
        return InternalGetUserAccessTokenResponse.class;
    }

    @Override
    public void check() throws ApiRuleException {
        if (StringUtils.isBlank(this.clientId)) {
            throw new ApiRuleException("-1", "获取用户token失败，缺少必要参数：clientId");
        }
    }

    @Override
    public String getTopContentType() {
        return this.topContentType;
    }

    @Override
    public void setTopContentType(String topContentType) {
        this.topContentType = topContentType;
    }

    @Override
    public String getRequestUrl() {
        return this.requestUrl;
    }
}
